﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using nanoCMS.Blog.Concrete;
using nanoCMS.Blog.Providers;
using nanoCMS.Blog.WebUI.Models.ViewModels;
using nanoCMS.Core;

namespace nanoCMS.Blog.WebUI.Models
{
    public class BlogPostService
    {
        private readonly WorldContext worldContext;
        private BlogPostProvider blogPostProvider;
        private BlogCategoryProvider blogCategoryProvider;

        public BlogPostService(WorldContext worldContext)
        {
            this.worldContext = worldContext;
            blogPostProvider = new BlogPostProvider(worldContext);
            blogCategoryProvider = new BlogCategoryProvider(worldContext);
        }

        public BlogPostVM Add(BlogPostVM model, int[] blogInCategories)
        {
            //List<int> blogInCat = blogInCategories.ToList();
            BlogPost blogPost;
            blogPostProvider.Add(model.Title, model.Summary, model.Content, model.CommentsAllowed, model.PublishedFrom,
                                 model.PublishedTo, model.Sticky, model.PrimaryImage, blogInCategories.ToList(), out blogPost);
            return BlogPostVmFactory(blogPost); 
        }


        public void Edit(BlogPostVM model, int[] blogInCategories)
        {
            //TODO: logic for editing blog in categories
            BlogPost blogPost;
            blogPostProvider.Edit(model.BlogPostID, model.Title, model.Summary, model.Content, model.CommentsAllowed,
                                  model.PublishedFrom, model.PublishedTo, model.Sticky, model.PrimaryImage, blogInCategories.ToList(),
                                  out blogPost);
        }

        public List<BlogPostVM> ListBlogPosts(int? categoryID = null, int? skip = null, int? take = null, int? userID = null, DateTime? publishedFrom = null, DateTime? publishedTo = null, int? tenantID = null)
        {
            return blogPostProvider.GetPosts(categoryID, skip, take, userID, publishedFrom, publishedTo, tenantID).Select(BlogPostVmFactory).ToList();
        }

        public EditBlogPostVM CreateEmptyEditBlogPostVM()
        {
            return new EditBlogPostVM
            {
                BlogPostVM = new BlogPostVM(),
                TenantCategories = blogCategoryProvider.GetCategoriesForTenant(
                    RequestItems.TenantID).ToDictionary(x => x.BlogCategoryID, x => x.Title),
                BlogPostCategories = new List<int>()
            };
        }

        public BlogPostVM GetBlogPost(int id)
        {
            BlogPost blogPost;
            if (blogPostProvider.GetBlogPost(id, out blogPost) != BlogReturn.Success) return null;
            return BlogPostVmFactory(blogPost);
        }

        // TODO generiranje već izabranih kategorija  (za već odabrane kategorije)
        public EditBlogPostVM GetEditBlogPostVM(int blogPostID)
        {
            BlogPost blogPost;
            blogPostProvider.GetBlogPost(blogPostID, out blogPost);
            return new EditBlogPostVM
            {
                BlogPostVM = BlogPostVmFactory(blogPost),
                TenantCategories = blogCategoryProvider.GetCategoriesForTenant(1).ToDictionary(
                    x => x.BlogCategoryID, x => x.Title),
                BlogPostCategories = new List<int>()
            };
        }

        #region Security/Validation 
        public bool CheckCategories(int[] categories)
        {
            if(categories != null && categories.Count() > 0)
            {
                return true; 
            }
            return false; 
        }
        #endregion

        #region Factories
        public static BlogPostVM BlogPostVmFactory(BlogPost blogPost)
        {
            //TODO: <worker> dovrsi
            return new BlogPostVM
                   {
                       BlogPostID = blogPost.BlogPostID,
                       Title = blogPost.Title,
                       Summary = blogPost.Summary,
                       Content = blogPost.Content,
                       CommentsAllowed = blogPost.CommentsAllowed,
                       CreatedBy = blogPost.CreatedBy,
                       CreatedDate = blogPost.CreatedDate,
                       ModifiedBy = blogPost.ModifiedBy,
                       ModifiedDate = blogPost.ModifiedDate
                   };
        }
        #endregion
    }
}
